import { TableColumnProps } from "ant-design-vue";
import {
  AdjustmentType,
  DeliveryType,
  GoodsSaleType,
  ProductFileItem,
  ProfitLossType,
  PurchaseOrderType,
  ReceiptItem,
  ReceiptType,
} from "/@/api/main/pis/models";
import { getLabelByValueFromEnum, handleDateRowRender } from "/@/utils/common";
import dayjs from "dayjs";
import { useI18n } from "/@/locales/useI18n";

const { t } = useI18n();

export interface ReceiptTableExpose {
  getProductData: () => Partial<ProductFileItem>[] | null;
}

export interface RelateOrderExpose {
  relateCurReceipt: (curReceipt: Partial<ReceiptItem>, relateData: any) => void;
}

export interface RelateOrderOptions {
  reset?: boolean;
  curReceipt?: Partial<ReceiptItem>;
}

export function useReceiptManage(receiptType: ReceiptType) {
  let addText = "",
    pageTitle = "",
    editText = "",
    deleteText = "",
    innerColumns: TableColumnProps<Partial<ReceiptItem>>[] = [];
  if (receiptType === ReceiptType.delivery) {
    addText = "新增商品出库单";
    pageTitle = "商品出库管理";
    editText = "编辑商品出货单";
    deleteText = "确认删除该出库单吗？";
    innerColumns = [
      {
        title: "出库单号",
        dataIndex: "deliveryNo",
      },
      {
        title: "出库单类型",
        dataIndex: "deliveryType",
        customRender({ text }) {
          return getLabelByValueFromEnum(text, DeliveryType, "deliveryType");
        },
      },
      {
        title: "仓库",
        dataIndex: "warehouse",
      },
      {
        title: "仓库管理员",
        dataIndex: "warehouseAdmin",
      },
      {
        title: "出库日期",
        dataIndex: "deliveryDate",
        customRender: handleDateRowRender,
      },
      {
        title: "制单时间",
        dataIndex: "tabulationTime",
        customRender: handleDateRowRender,
      },
      {
        title: "审核时间",
        dataIndex: "reviewTime",
        customRender: handleDateRowRender,
      },
      {
        title: "出库数量",
        dataIndex: "deliveryQuantity",
      },
      {
        title: "出库金额",
        dataIndex: "deliveryAmount",
      },
      {
        title: "状态",
        dataIndex: "status",
      },
      {
        title: t("common.oper"),
        key: "oper",
        fixed: "right",
        width: 100,
      },
    ];
  } else if (receiptType === ReceiptType.warehousing) {
    addText = "新增商品入库单";
    pageTitle = "商品入库管理";
    editText = "编辑商品入库单";
    deleteText = "确认删除该入库单吗？";
    innerColumns = [
      {
        title: "入库单号",
        dataIndex: "warehousingNo",
      },
      // {
      //   title: "关联订单号",
      //   dataIndex: "x",
      // },
      {
        title: "供应商",
        dataIndex: "supplierName",
      },
      {
        title: "仓库",
        dataIndex: "warehouse",
      },
      {
        title: "仓库管理员",
        dataIndex: "warehouseAdmin",
      },
      {
        title: "入库日期",
        dataIndex: "warehousingDate",
        customRender: handleDateRowRender,
      },
      {
        title: "制单时间",
        dataIndex: "tabulationTime",
        customRender: handleDateRowRender,
      },
      {
        title: "审核时间",
        dataIndex: "reviewTime",
        customRender: handleDateRowRender,
      },
      {
        title: "入库数量",
        dataIndex: "warehousingQuantity",
      },
      {
        title: "入库金额",
        dataIndex: "warehousingAmount",
      },
      {
        title: "状态",
        dataIndex: "status",
      },
      {
        title: t("common.oper"),
        key: "oper",
        fixed: "right",
        width: 100,
      },
    ];
  } else if (receiptType === ReceiptType.return) {
    addText = "新增商品退货单";
    pageTitle = "商品退货管理";
    editText = "编辑商品退货单";
    deleteText = "确认删除该退货单吗？";
    innerColumns = [
      {
        title: "退货单号",
        dataIndex: "returnsNo",
      },
      {
        title: "退回仓库",
        dataIndex: "returnsWarehouse",
      },
      {
        title: "仓库管理员",
        dataIndex: "warehouseAdmin",
      },
      {
        title: "退货日期",
        dataIndex: "returnsDate",
        customRender: handleDateRowRender,
      },
      {
        title: "制单时间",
        dataIndex: "tabulationTime",
        customRender: handleDateRowRender,
      },
      {
        title: "审核时间",
        dataIndex: "reviewTime",
        customRender: handleDateRowRender,
      },
      {
        title: "退货数量",
        dataIndex: "returnsQuantity",
      },
      {
        title: "退货金额",
        dataIndex: "returnsAmount",
      },
      {
        title: "状态",
        dataIndex: "status",
      },
      {
        title: t("common.oper"),
        key: "oper",
        fixed: "right",
        width: 100,
      },
    ];
  } else if (receiptType === ReceiptType.adjustment) {
    addText = "新增商品调拨单";
    pageTitle = "商品调拨管理";
    editText = "编辑商品调拨单";
    deleteText = "确认删除该调拨单吗？";
    innerColumns = [
      {
        title: "调拨单号",
        dataIndex: "adjustmentNo",
      },
      {
        title: "调拨单类型",
        dataIndex: "adjustmentType",
        customRender({ text }) {
          return getLabelByValueFromEnum(text, AdjustmentType, "adjustmentType");
        },
      },
      {
        title: "出货仓库",
        dataIndex: "deliveryWarehouse",
      },
      {
        title: "出货管理员",
        dataIndex: "deliveryAdmin",
      },
      {
        title: "收货仓库",
        dataIndex: "receiveWarehouse",
      },
      {
        title: "收货管理员",
        dataIndex: "receiveAdmin",
      },
      {
        title: "调拨日期",
        dataIndex: "adjustmentDate",
        customRender: handleDateRowRender,
      },
      {
        title: "制单时间",
        dataIndex: "tabulationTime",
        customRender: handleDateRowRender,
      },
      {
        title: "审核时间",
        dataIndex: "reviewTime",
        customRender: handleDateRowRender,
      },
      {
        title: "调拨数量",
        dataIndex: "adjustmentQuantity",
      },
      {
        title: "调拨金额",
        dataIndex: "adjustmentAmount",
      },
      {
        title: "状态",
        dataIndex: "status",
      },
      {
        title: t("common.oper"),
        key: "oper",
        fixed: "right",
        width: 100,
      },
    ];
  } else if (receiptType === ReceiptType.profitLoss) {
    addText = "新增商品损益单";
    pageTitle = "商品损益管理";
    editText = "编辑商品损益单";
    deleteText = "确认删除该损益单吗？";
    innerColumns = [
      {
        title: "损益单号",
        dataIndex: "profitLossNo",
      },
      {
        title: "损益单类型",
        dataIndex: "profitLossType",
        customRender({ text }) {
          return getLabelByValueFromEnum(text, ProfitLossType, "profitLossType");
        },
      },
      {
        title: "仓库",
        dataIndex: "warehouse",
      },
      {
        title: "仓库管理员",
        dataIndex: "warehouseAdmin",
      },
      {
        title: "损益日期",
        dataIndex: "profitLossDate",
        customRender: handleDateRowRender,
      },
      {
        title: "制单时间",
        dataIndex: "tabulationTime",
        customRender: handleDateRowRender,
      },
      {
        title: "审核时间",
        dataIndex: "reviewTime",
        customRender: handleDateRowRender,
      },
      {
        title: "损益数量",
        dataIndex: "profitLossQuantity",
      },
      {
        title: "损益金额",
        dataIndex: "profitLossAmount",
      },
      {
        title: "状态",
        dataIndex: "status",
      },
      {
        title: t("common.oper"),
        key: "oper",
        fixed: "right",
        width: 100,
      },
    ];
  } else if (receiptType === ReceiptType.purchase) {
    addText = "新增采购订单";
    pageTitle = "采购订单管理";
    editText = "编辑采购订单";
    deleteText = "确认删除该采购订单吗？";
    innerColumns = [
      {
        title: "订单号",
        dataIndex: "orderNo",
      },
      {
        title: "供应商",
        dataIndex: "supplierName",
      },
      {
        title: "订单类型",
        dataIndex: "orderType",
        customRender({ text }) {
          return getLabelByValueFromEnum(text, PurchaseOrderType, "purchaseOrderType");
        },
      },
      {
        title: "送货日期",
        dataIndex: "deliveryDate",
        customRender: handleDateRowRender,
      },
      {
        title: "订货数量",
        dataIndex: "orderQuantity",
      },
      {
        title: "订货金额",
        dataIndex: "orderAmount",
      },
      {
        title: "采购员",
        dataIndex: "purchasingOfficer",
      },
      {
        title: "仓库",
        dataIndex: "warehouse",
      },
      {
        title: "制单时间",
        dataIndex: "tabulationTime",
        customRender: handleDateRowRender,
      },
      {
        title: "审核时间",
        dataIndex: "reviewTime",
        customRender({ text }) {
          return text ? dayjs(text).format("YYYY-MM-DD HH:mm:ss") : "--";
        },
      },
      {
        title: "状态",
        dataIndex: "status",
      },
      {
        title: t("common.oper"),
        key: "oper",
        fixed: "right",
        width: 100,
      },
    ];
  } else if (receiptType === ReceiptType.sell) {
    addText = "新增商品销售单";
    pageTitle = "商品销售单管理";
    editText = "编辑商品销售单";
    deleteText = "确认删除该商品销售单吗？";
    innerColumns = [
      {
        title: "销售单号",
        dataIndex: "goodsSaleNo",
      },
      {
        title: "销售类型",
        dataIndex: "goodsSaleType",
        customRender({ text }) {
          return getLabelByValueFromEnum(text, GoodsSaleType, "goodsSaleType");
        },
      },
      {
        title: "出货仓库",
        dataIndex: "warehouse",
      },
      {
        title: "仓库管理员",
        dataIndex: "warehouseAdmin",
      },
      {
        title: "销售日期",
        dataIndex: "saleDate",
        customRender: handleDateRowRender,
      },
      {
        title: "制单时间",
        dataIndex: "tabulationTime",
        customRender: handleDateRowRender,
      },
      {
        title: "审核时间",
        dataIndex: "reviewTime",
        customRender: handleDateRowRender,
      },
      {
        title: "销售数量",
        dataIndex: "saleQuantity",
      },
      {
        title: "销售金额",
        dataIndex: "saleAmount",
      },
      {
        title: "状态",
        dataIndex: "status",
      },
      {
        title: t("common.oper"),
        key: "oper",
        fixed: "right",
        width: 100,
      },
    ];
  }

  return {
    addText,
    pageTitle,
    editText,
    deleteText,
    innerColumns,
  };
}
